﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Office2010.Excel;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.Common;
using NP.BPMReportPlatform.HR.Dto;
using NP.BPMReportPlatform.RecurringJobs.Dto;
using NP.BPMReportPlatform.ZHLC.Dto;
using NPOI.SS.Formula.Functions;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.HR
{
    /// <summary>
    /// 人事 接口
    /// </summary>
    //[Authorize]
    [ApiController]
    [Route("api/hr")]
    public class HRAppController : BPMReportPlatformController
    {
        private readonly IHRAppService _hRService;

        public HRAppController(IHRAppService hRService)
        {
            _hRService = hRService;
        }
        #region BPM审批配置维护
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ApproveConfig/GetAllAsync")]
        public async Task<PagedResultDto<GetApproveConfigOutput>> GetAllAsync([FromBody] GetApproveConfigInput input)
        {
            return await _hRService.GetAllAsync(input);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ApproveConfig/SaveAsync")]
        public async Task<string> SaveAsync([FromBody] SaveApproveConfigInput input)
        {
            var res = "成功";
            GetApproveConfigInput getApproveConfig = new GetApproveConfigInput()
            {
                ProcessName = input.ProcessName,
                Rank = input.Rank,
                CatalogName = input.CatalogName,
                PageIndex = 1,
                PageSize = 1
            };
            var data = await GetAllAsync(getApproveConfig);
            if (data != null && data.TotalCount > 0)
            {
                res = "存在重复的'所属流程-职级-归属分类'！";
            }
            else
            {
                await _hRService.SaveAsync(input);
            }
            return res;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ApproveConfig/EditAsync")]
        public async Task EditAsync([FromBody] SaveApproveConfigInput input)
        {
            if (input.Id != Guid.Empty)
            {
                await _hRService.EditAsync(input);
            }
        }

        /// <summary>
        /// 删除审批配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("ApproveConfig/DeleteAsync")]
        public async Task DeleteAsync([FromQuery] Guid id)
        {
            await _hRService.DeleteAsync(id);
        }

        /// <summary>
        /// BPM审批配置维护  导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ApproveConfig/ExportAsync")]
        public async Task<XlsxFileResult<GetApproveConfigOutput>> ExportAsync([FromBody] GetApproveConfigInput input)
        {
            var rst = await _hRService.ExportAsync(input);
            return new XlsxFileResult<GetApproveConfigOutput>(rst, "导出Excel");
        }

        /// <summary>
        /// 批量导入Excel
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ApproveConfig/ImporterExcelAsync")]
        public async Task<ImportResultExt<ImportApproveConfigInput>> ImporterExcelAsync(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<ImportApproveConfigInput>();
                res.Exception = new Exception("请上传文件！");
                return res;
            }
            var s = file.OpenReadStream();
            var bytes = StreamToBytes(s);
            //var file = Request.Form.Files[0];
            return await _hRService.ImporterExcel(bytes, "add");
        }
        /// <summary>
        /// 根据员工工号获取审批信息
        /// </summary>
        /// <param name="workNumber"></param>
        [HttpGet]
        [Route("ApproveConfig/GetHREmployeeApprover")]
        public async Task<List<UserFetch2Dto>> GetHREmployeeApprover([FromQuery] string workNumber)
        {
            return await _hRService.GetHREmployeeApprover(workNumber);
        }

        /// <summary>
        /// 根据员工号获取员工利益审批人
        /// </summary>
        /// <param name="workNumber"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("ApproveConfig/GetEmployeeBenefitsApprover")]
        public List<UserFetchDto> GetEmployeeBenefitsApprover([FromQuery] string workNumber)
        {
            return _hRService.GetEmployeeBenefitsApprover(workNumber);
        }

        /// <summary>
        /// 开放给老BPM的接口，根据UserId从SFOtherApproveEntity获取员工关系和行政
        /// </summary>
        /// <param name="userId"></param>
        [HttpGet]
        [Route("ApproveConfig/GetSFOtherApproveRelUserId")]
        public async Task<List<GetSFOtherApproveRelOutput>> GetSFOtherApproveRelUserId([FromQuery] string userId)
        {
            return await _hRService.GetSFOtherApproveRelUserId(userId);
        }

        [HttpGet]
        [Route("ApproveConfig/GetUserRegion")]
        public async Task<GetUserRegionOutput> GetUserRegion(string sapEmployeeId)
        {
            return await _hRService.GetUserRegionInfo(sapEmployeeId);
        }
        #endregion

        #region 手机费额度配置维护
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PhoneFeeLimitConfig/GetAllAsync")]
        public async Task<PagedResultDto<GetPhoneFeeLimitConfigOutput>> GetAllAsync([FromBody] GetPhoneFeeLimitConfigInput input)
        {
            return await _hRService.GetAllAsync(input);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PhoneFeeLimitConfig/SaveAsync")]
        public async Task<string> SaveAsync([FromBody] SavePhoneFeeLimitConfigInput input)
        {
            var res = "成功";
            GetPhoneFeeLimitConfigInput getApproveConfig = new GetPhoneFeeLimitConfigInput()
            {
                Type = input.Type,
                Rank = input.Rank,
                RankName = input.RankName,
                PageIndex = 1,
                PageSize = 1
            };
            await _hRService.SaveAsync(input);
            return res;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PhoneFeeLimitConfig/EditAsync")]
        public async Task EditAsync([FromBody] SavePhoneFeeLimitConfigInput input)
        {
            if (input.Id != Guid.Empty)
            {
                await _hRService.EditAsync(input);
            }
        }

        /// <summary>
        /// 删除配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("PhoneFeeLimitConfig/DeleteAsync")]
        public async Task DeletePhoneFeeLimitConfigAsync([FromQuery] Guid id)
        {
            await _hRService.DeletePhoneFeeLimitConfigAsync(id);
        }

        /// <summary>
        /// 配置维护  导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PhoneFeeLimitConfig/ExportAsync")]
        public async Task<XlsxFileResult<GetPhoneFeeLimitConfigOutput>> ExportAsync([FromBody] GetPhoneFeeLimitConfigInput input)
        {
            var rst = await _hRService.ExportAsync(input);
            return new XlsxFileResult<GetPhoneFeeLimitConfigOutput>(rst, "导出Excel");
        }

        /// <summary>
        /// 话费额度  导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PhoneFeeLimitConfig/Export2Async")]
        public async Task<XlsxFileResult<UserLimitDto>> Export2Async()
        {
            var rst = await _hRService.GetPhoneFeeLimitConfigInfo();
            return new XlsxFileResult<UserLimitDto>(rst, "导出Excel");
        }

        /// <summary>
        /// 批量导入Excel
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PhoneFeeLimitConfig/ImporterExcelAsync")]
        public async Task<ImportResultExt<ImportPhoneFeeLimitConfigInput>> ImporterPhoneFeeLimitExcelAsync(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<ImportPhoneFeeLimitConfigInput>();
                res.Exception = new Exception("请上传文件！");
                return res;
            }
            var s = file.OpenReadStream();
            var bytes = StreamToBytes(s);
            //var file = Request.Form.Files[0];
            return await _hRService.ImporterPhoneFeeLimitExcel(bytes, "add");
        }
        /// <summary>
        /// 获取所有人的手机费额度信息
        /// </summary>
        [HttpGet]
        [Route("GetPhoneFeeLimitConfigInfo")]
        public async Task<List<UserLimitDto>> GetPhoneFeeLimitConfigInfo()
        {
            return await _hRService.GetPhoneFeeLimitConfigInfo();
        }
        #endregion

        #region 交通费额度配置维护
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("TrafficFeeLimitConfig/GetAllAsync")]
        public async Task<PagedResultDto<GetTrafficFeeLimitConfigOutput>> GetAllAsync([FromBody] GetTrafficFeeLimitConfigInput input)
        {
            return await _hRService.GetAllAsync(input);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("TrafficFeeLimitConfig/SaveAsync")]
        public async Task<string> SaveAsync([FromBody] SaveTrafficFeeLimitConfigInput input)
        {
            var res = "成功";
            await _hRService.SaveAsync(input);
            return res;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("TrafficFeeLimitConfig/EditAsync")]
        public async Task EditAsync([FromBody] SaveTrafficFeeLimitConfigInput input)
        {
            if (input.Id != Guid.Empty)
            {
                await _hRService.EditAsync(input);
            }
        }
        /// <summary>
        /// 一键同步，推送数据到费控
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("TrafficFeeLimitConfig/PushDataAsync")]
        public async Task<string> PushTrafficFeeLimitsync(Guid id)
        {
           return await _hRService.PushTrafficFeeLimitAsync(id);
        }
        /// <summary>
        /// 删除配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("TrafficFeeLimitConfig/DeleteAsync")]
        public async Task DeleteTrafficFeeLimitConfigAsync([FromQuery] Guid id)
        {
            await _hRService.DeleteTrafficFeeLimitConfigAsync(id);
        }

        /// <summary>
        /// 配置维护  导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("TrafficFeeLimitConfig/ExportAsync")]
        public async Task<XlsxFileResult<GetTrafficFeeLimitConfigOutput>> ExportAsync([FromBody] GetTrafficFeeLimitConfigInput input)
        {
            var rst = await _hRService.ExportAsync(input);
            return new XlsxFileResult<GetTrafficFeeLimitConfigOutput>(rst, "导出Excel");
        }

        /// <summary>
        /// 交通费额度  导出数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("TrafficFeeLimitConfig/Export2Async")]
        public async Task<XlsxFileResult<UserLimitDto>> ExportTrafficFeeLimitAsync()
        {
            var rst = await _hRService.GetTrafficFeeLimitConfigInfo();
            return new XlsxFileResult<UserLimitDto>(rst, "导出Excel");
        }
        /// <summary>
        /// 交通费额度配置批量导入Excel
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("TrafficFeeLimitConfig/ImporterExcelAsync")]
        public async Task<ImportResultExt<ImportTrafficFeeLimitConfigInput>> ImporterTrafficFeeLimitExcelAsync(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<ImportTrafficFeeLimitConfigInput>();
                res.Exception = new Exception("请上传文件！");
                return res;
            }
            var s = file.OpenReadStream();
            var bytes = StreamToBytes(s);
            //var file = Request.Form.Files[0];
            return await _hRService.ImporterTrafficFeeLimitExcel(bytes);
        }

        /// <summary>
        /// 获取所有人的交通费额度信息
        /// </summary>
        [HttpGet]
        [Route("GetTrafficFeeLimitConfigInfo")]
        public async Task<List<UserLimitDto>> GetTrafficFeeLimitConfigInfo()
        {
            return await _hRService.GetPhoneFeeLimitConfigInfo();
        }
        #endregion

        #region HR费控系统前置审批
        /// <summary>
        ///HR费控系统前置审批 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("HRFKXTQZSP/GetAllAsync")]
        public async Task<PagedResultDto<GetHRFKXTQZSPOutput>> GetAllAsync([FromBody] GetHRFKXTQZSPInput input)
        {
            return await _hRService.GetAllAsync(input);
        }

        /// <summary>
        /// HR费控系统前置审批  导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("HRFKXTQZSP/ExportAsync")]
        public async Task<XlsxFileResult<GetHRFKXTQZSPOutput>> ExportAsync([FromBody] GetHRFKXTQZSPInput input)
        {

            var rst = await _hRService.ExportAsync(input);
            return new XlsxFileResult<GetHRFKXTQZSPOutput>(rst, "导出Excel");
        }
        #endregion
        /// <summary>
        /// 获取用户数据权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetUserDataPermissions")]
        public async Task<List<string>> GetUserDataPermissions([FromQuery] string EmployeeCode)
        {
            return await _hRService.GetUserDataPermissionsForDept(EmployeeCode);
        }
        /// <summary>
        /// 推送数据到费控
        /// </summary>
        /// <param name="dataList"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SyncTrafficFee")]
        public async Task<string> SyncTrafficFee([FromBody]List<TrafficFeePushDataDto> dataList)
        {
            return await _hRService.SyncTrafficFee(dataList);
        }

        private byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
    }
}